<template>
	<Layout showFooter :innerBottom="true">
		<template #title>订单详情</template>

		<view class="iflex column asc detail-section">
			<view class="iflex fill title">
				订单号:{{detail.orderNumber}}
			</view>
			<view class="iflex jcsb aic fill desc">
				<text>服务类型</text>
				<text>{{detail.serviceType}}</text>
			</view>
			<view class="iflex jcsb aic fill desc">
				<text>订单状态</text>
				<text>{{dispatchStatusFormat(detail.dispatchStatus)}}</text>
			</view>
			<view class="iflex jcsb aic fill desc">
				<text>联系电话</text>
				<text>{{detail.contactPhone}}</text>
			</view>
			<view class="iflex jcsb aic fill desc">
				<text>用户名称</text>
				<text>{{detail.userName}}</text>
			</view>
			<view class="iflex jcsb aic fill desc">
				<text>备注</text>
				<text></text>
			</view>
			<view class="iflex jcsb aic fill desc remark">
				{{detail.orderRemark}}
			</view>
		</view>

		<view class="iflex column asc normal-section">
			<view class="iflex jcsb aic fill desc">
				<text>订单号</text>
				<view class="iflex aic ">
					{{detail.orderNumber}}
					<image @click.stop="tapCopyOrderNumber" src="/static/image/common/copy.png"
						style="width:30rpx; height: 29rpx;margin-left: 15rpx;">
					</image>
				</view>
			</view>
			<view class="iflex jcsb aic fill desc">
				<text>下单时间</text>
				<text>{{detail.orderTime}}</text>
			</view>
			<view class="iflex jcsb aic fill desc">
				<text>付款时间</text>
				<text>{{detail.paymentTime}}</text>
			</view>
			<view v-if="detail.dispatchStatus === DispatchStatus.Finished" class="iflex jcsb aic fill desc">
				<text>完成时间</text>
				<text>{{detail.finishedTime}}</text>
			</view>
		</view>

		<template #footer>
			<view class="iflex jcc fill detail-footer">
				<u-button v-if="detail.dispatchStatus === DispatchStatus.Processing" type="primary" size="large"
					:custom-style="{width:'686rpx'}">接单</u-button>
				<u-button v-if="detail.orderStatus === DispatchStatus.Confirmed" type="primary" size="large"
					:custom-style="{width:'686rpx'}" @click="tapApplayFinish">申请完成</u-button>
			</view>
		</template>
	</Layout>
</template>

<script setup lang="ts">
	import type { AnyObj } from '@/types/global';
	import { onLoad } from '@dcloudio/uni-app';
	import { ref } from 'vue';
	import { getAssignDetail, completedOrder } from '@/apis/trade';
	import { dispatchStatusFormat } from '@/utils/format';
	import { DispatchStatus, OrderStatus } from '@/utils/constant';
	import { isResultSuccess } from '@/utils/http';

	let orderId = '';
	let trackingNumber = '';
	const detail = ref<AnyObj>({});

	function tapCopyOrderNumber() {
		uni.setClipboardData({
			data: detail.value.orderNumber,
			success: function () {
				uni.showToast({
					title: '复制成功',
					icon: 'none'
				})
			}
		})
	}



	function loadData() {
		detail.value = {};
		getAssignDetail(trackingNumber).then((res) => {
			detail.value = res.data ?? {};
		});
	}

	function tapApplayFinish() {
		completedOrder(detail.value.trackingNumber).then((res) => {
			if (!isResultSuccess(res, '申请成功')) return;
			setTimeout(() => {
				loadData();
			}, 1500);
		});
	}

	onLoad((options) => {
		orderId = options!.orderId as string;
		trackingNumber = options!.trackingNumber as string;
		loadData();
	});
</script>

<style lang="scss">
	.detail-section {
		margin-top: 30rpx;
		padding: 38rpx 20rpx 54rpx 20rpx;
		width: 686rpx;
		border-radius: 16rpx;
		border: 1px solid #FFF;
		background: linear-gradient(180deg, #FEEDE0 -4.43%, #FFF 12.73%);

		.title {
			padding: 0 0 14rpx 0;
			color: #000000;
			font-size: 32rpx;
			font-weight: 500;
		}

		.desc {
			margin-top: 32rpx;
			min-height: 42rpx;
			color: #4b5466;
			font-size: 30rpx;
			font-weight: 400;


			text:last-child {
				color: #101010;
			}

			&.remark {
				margin-top: 24rpx;
				font-size: 28rpx;
			}
		}
	}

	.normal-section {
		margin-top: 24rpx;
		padding: 6rpx 20rpx 38rpx 20rpx;
		width: 686rpx;
		border-radius: 8rpx;
		background: #FFF;

		.desc {
			margin-top: 32rpx;
			min-height: 42rpx;
			color: #4b5466;
			font-size: 30rpx;
			font-weight: 400;
		}
	}

	.detail-footer {
		padding: 32rpx 0;
		background-color: #F6F6F6;
	}
</style>